#include <rtthread.h>
#include <board.h>
#include "bl_if.h"

#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
#include <finsh.h>
#include <shell.h>
#endif

#define DBG_ENABLE
#define DBG_SECTION_NAME                    "main"

#ifdef RT_MAIN_DEBUG
#define DBG_LEVEL                           DBG_LOG
#else
#define DBG_LEVEL                           DBG_INFO
#endif

#define DBG_COLOR
#include <rtdbg.h>

const char boot_log[] = 
"\
  _____        ____   ___   ___   _____  \r\n\
 |_   _|      | _  \\ / _ \\ / _ \\ |_   _| \r\n\
   | |  _____ ||_| | || || || ||   | |   \r\n\
   | |  _____ | _ <  || || || ||   | |   \r\n\
   | |        ||_| | ||_|| ||_||   | |   \r\n\
   |_|        |____/ \\___/ \\___/   |_|   \r\n\
";

static void _print_boot_log(void)
{
    LOG_RAW("%s", boot_log);
	LOG_RAW("2021 - 2021 Copyright by T \r\n");
	LOG_RAW("Version: %s; Build: %s\r\n\r\n", TBOOT_VERSION_STR, __DATE__);
}

int main(void)
{
    rt_err_t ret = -RT_ETIMEOUT;
    int i = 0;

    // init flash partition
    initOnChipFlashPartition();
    initHoldKey();

    _print_boot_log();
#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
	finsh_set_prompt("tboot />");
#endif

#if 1
    if (findAppPartition() == RT_FALSE) {
        // no partition, do nothing
    } else if (holdBootloader() == RT_TRUE) {   // enter update
        if (updateApplication() == RT_TRUE) {
            jump2Application();
        }
    } else {                                    // enter boot application
        jump2Application();
    }

#endif

while (1) {
    rt_thread_mdelay(1);
}

    return RT_EOK;
}
